package com.shanshan.crowd.controller;

import com.shanshan.crowd.api.MySQLRemoteService;
import com.shanshan.crowd.constant.CrowdConstant;
import com.shanshan.crowd.entity.vo.AddressVO;
import com.shanshan.crowd.entity.vo.MemberLoginVO;
import com.shanshan.crowd.entity.vo.OrderProjectVO;
import com.shanshan.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description
 * @ClassName OrderController
 * @Author 山山大魔王
 * @Date 2022/8/1 17:06
 * @Version 1.0
 */
@Controller
public class OrderController {

    @Autowired
    MySQLRemoteService mySQLRemoteService;



    @RequestMapping("/save/address")
    public String saveAddress(AddressVO addressVO,HttpSession session){

        // 1.执行地址信息的保存
        ResultEntity<String> resultEntity = mySQLRemoteService.saveAddressRemote(addressVO);

        // 2.从Session域获取orderProjectVO对象
        OrderProjectVO orderProjectVO =(OrderProjectVO) session.getAttribute("orderProjectVO");

        // 3.从orderProjectVO对象中获取returnCount
        Integer returnCount = orderProjectVO.getReturnCount();

        // 4.重定向到确认表单页面
        return "redirect:http://localhost/order/confirm/order/"+returnCount;
    }

    @RequestMapping("/confirm/order/{returnCount}")
    public String showConfirmOrderInfo(@PathVariable("returnCount") Integer returnCount,
                                       HttpSession session){

        // 1.把接收到的回报数据量合并到Session域
        OrderProjectVO orderProjectVO = (OrderProjectVO)session.getAttribute("orderProjectVO");

        orderProjectVO.setReturnCount(returnCount);

        session.setAttribute("orderProjectVO", orderProjectVO);

        // 2.获取当前已经登录用户的id
        MemberLoginVO memberLoginVO = (MemberLoginVO)session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);

        Integer memberId = memberLoginVO.getId();

        // 3.查询目前的收获地址数据
        ResultEntity<List<AddressVO>> resultEntity = mySQLRemoteService.getAddressVORemote(memberId);

        if (ResultEntity.SUCCESS.equals(resultEntity.getOperationResult())) {

            List<AddressVO> list = resultEntity.getQueryData();

            session.setAttribute("addressVOList", list);
        }

        return "confirm_order";
    }

//    <a class="btn btn-warning btn-lg" th:href="'http://localhost/order/confirm/return/info/' + ${return.returnId}">
    @RequestMapping("/confirm/return/info/{returnId}")
    public String showReturnConfirmInfo(@PathVariable("returnId") Integer returnId, HttpSession session) {
        ResultEntity<OrderProjectVO> resultEntity = mySQLRemoteService.getOrderProjectVORemote( returnId);
        if(ResultEntity.SUCCESS.equals(resultEntity.getOperationResult())) {
            OrderProjectVO orderProjectVO = resultEntity.getQueryData();
// 为了能够在后续操作中保持 orderProjectVO 数据，存入 Session 域
            session.setAttribute("orderProjectVO", orderProjectVO);
        }
        return "confirm_return";
    }

}
